package jDialog;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;

public class DeletePastGroup extends JDialog {
	private static final long serialVersionUID = 1L;

	private JTable table;
	private File[] files;
	private String path;

	public DeletePastGroup(JFrame frame, String path) {
		super(frame, "Delete Past Groups", ModalityType.DOCUMENT_MODAL);

		this.path = path;
		this.addTopPanel();
		this.addBottomPanel();
		this.pack();
		this.setDialogPos();
		this.setVisible(true);
	}

	public void addBottomPanel() {
		JButton bttnCancel = new JButton("Cancel");
		bttnCancel.setPreferredSize(new Dimension(90, 26));
		bttnCancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				DeletePastGroup.this.dispose();
			}
		});
		JButton bttnDelete = new JButton("Delete");
		bttnDelete.setPreferredSize(new Dimension(90, 26));
		bttnDelete.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				for (int a = files.length - 1; a >= 0; a--)
					if (table.getModel().getValueAt(a, 1) != null && (boolean) table.getModel().getValueAt(a, 1) == true)
						files[a].delete();
				DeletePastGroup.this.dispose();
			}
		});
		JButton bttnDeleteAll = new JButton("Delete All");
		bttnDeleteAll.setPreferredSize(new Dimension(90, 26));
		bttnDeleteAll.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				for (int a = files.length - 1; a >= 0; a--) {
					files[a].delete();
				}
				DeletePastGroup.this.dispose();
			}
		});
		JPanel pBottom = new JPanel();
		pBottom.add(bttnCancel);
		pBottom.add(bttnDelete);
		pBottom.add(bttnDeleteAll);
		DeletePastGroup.this.add(pBottom, BorderLayout.SOUTH);
	}

	public void addTopPanel() {
		String[] colNames = { "Groups", "Remove" };
		files = new File(path + "PastGroups").listFiles();
		Object[][] data = new Object[files.length][colNames.length];
		for (int a = 0; a < files.length; a++) {
			String temp = files[a].getName();
			data[a][0] = temp.subSequence(0, temp.indexOf("."));
		}
		table = new JTable(data, colNames) {
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return column != 0;
			}
		};
		table.getColumnModel().getColumn(0).setPreferredWidth(175);
		table.getColumnModel().getColumn(1).setPreferredWidth(75);
		table.getColumnModel().getColumn(1).setCellEditor(table.getDefaultEditor(Boolean.class));
		table.getColumnModel().getColumn(1).setCellRenderer(table.getDefaultRenderer(Boolean.class));
		JScrollPane scrollTable = new JScrollPane(table);
		scrollTable.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		table.setFillsViewportHeight(true);
		scrollTable.setPreferredSize(new Dimension(275, 200));

		JPanel top = new JPanel();
		top.add(scrollTable);

		DeletePastGroup.this.add(top);
	}

	private void setDialogPos() {
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (int) ((d.getWidth() - this.getWidth()) / 2);
		int y = (int) ((d.getHeight() - this.getHeight()) / 2);
		this.setLocation(x, y);
	}
}
